/*
 * @Author: tanjunyi@doctorwork.com
 * @Date: 2020-12-05 16:38:30
 * @LastEditors: tanjunyi@doctorwork.com
 * @LastEditTime: 2020-12-07 22:43:18
 * @Description:
 */
import { observable } from "mobx"
import { message } from 'antd';
import { jsonfy, jsonStr } from './utils';
const ipcRenderer = window.require('electron').ipcRenderer;


const project = observable({
	isSave: true,
	projectActive: 0,
	pageActive: 0,
	testActive: 0,
	eventActive: 0,
	tempIndex: 0,
	projects: [],
	code: '',
	/**
	 * 新建工程
	 * @param {*} config 
	 * projectName, path,cover
	 */
	createProject(config) {
		this.projects[this.projectActive].pages.push(config)
	},
	/**
	 * 新建页面
	 */
	createPage() {
		this.projects[this.projectActive].pages.push({
			pageName: '请输入页面名称',
			tests: []
		})

	},
	/**
	 * 新建测试用例
	 */
	createTest() {
		this.tempIndex += 1;
		this.projects[this.projectActive].pages[this.pageActive].tests.push(
			{
				title: `test_${this.tempIndex}`,
				events: []
			}
		)
	},
	changeActive(key, value) {
		if (typeof this[key] !== undefined) {
			this[key] = value
		}
	},
	// 删除测试用例
	deleteTest(index) {
		this.projects[this.projectActive].pages[this.pageActive].tests.splice(index, 1)
	},
	//删除event执行
	deleteEvent(testIndex, index) {
		this.projects[this.projectActive].pages[this.pageActive].tests[testIndex].events.splice(index, 1)

	},
	/**
	 * 修改页面名称
	 * @param {*} name 
	 */
	changePageName(name) {
		this.projects[this.projectActive].pages[this.pageActive].pageName = name;
	},
	/**
	 * 修改页面路径
	 * @param {*} name 
	 */
	changePagePath(name) {
		this.projects[this.projectActive].pages[this.pageActive].pagePath = name;
	},

	/**
	 * 修改页面名称
	 * @param {*} name 
	 */
	changeTestName(name, index) {
		this.projects[this.projectActive].pages[this.pageActive].tests[index].title = name
	},

	selectElement(idStr, eventIndex) {
		if (!idStr) return;
		const ids = idStr.split('-');
		if (ids[0]) {
			const pages = ids[0].split('_');
			this.changeActive('pageActive', pages[1]);
		}
		if (ids[1]) {
			const tests = ids[1].split('_');
			this.changeActive('testActive', tests[1]);
		}
		if (typeof eventIndex !== 'undefined') {
			this.eventActive = eventIndex
		}

		let element = document.querySelector(`#${idStr}`);
		let workspace = document.querySelector(`.workspace`);

		if (workspace && element) workspace.scrollTo(0, element.offsetTop - 100);

		// changeActive('pageActive', 1);
	},
	/**
	 * 添加执行事件
	 * @param {*} event 
	 */
	addEvent(event) {
		this.projects[this.projectActive].pages[this.pageActive].tests[this.testActive].events.push(event)
	},
	/**
	 * 保存
	 */
	save() {
		ipcRenderer.send('save-project', jsonStr(this.projects));
		console.log("正在保存")
	},
	/**
	 * 导出代码
	 */
	export() {
		if (!this.code) {
			return message.error('暂无内容可以导出');
		}
		const data = {
			fileName: this.projects[this.projectActive].projectName,
			content: this.code
		}
		ipcRenderer.send('export-code', data);

	},

	deletePage() {
		this.projects[this.projectActive].pages.splice(this.pageActive, 1);
		this.pageActive = 0;
	},
	/**
	 * 初始化数据
	 * @param {*} data 
	 */
	initData(data) {
		this.projects = data;
	},
	/**
	 * 执行测试用例
	 */
	runTest() {
		let test = this.projects[this.projectActive].pages[this.pageActive].tests[this.testActive];
		test = jsonfy(test)
		test.pagePath = this.projects[this.projectActive].pages[this.pageActive].pagePath;
		if (!test) return message.error('测试用例错误');
		console.log("开始执行测试用例：", test)
		ipcRenderer.send('run-test', test);
	},
	/**
	 * 
	 * @param {*} direction 
	 * @param {*} type 
	 */
	move(direction, type) {
		if (!~[1, -1].indexOf(direction)) throw Error("direction 取值错误");
		if (type === 'test') {

		} else if (type === 'event') {
			let events = this.projects[this.projectActive].pages[this.pageActive].tests[this.testActive].events;
			// 检查是否溢出
			let distActive = this.eventActive + direction;
			if (distActive < 0 || distActive >= events.length) return;
			// 执行交换
			let temp = events[this.eventActive];
			events[this.eventActive] = events[distActive];
			events[distActive] = temp;
			this.eventActive = distActive;
		}
	}

})

ipcRenderer.send('init-data');
// const project = new Project()
ipcRenderer.on('save-project-success', () => {
	message.success('添加成功');
})
ipcRenderer.on('export-code-success', () => {
	message.success('导出成功');
})

ipcRenderer.on('init-data-success', (sender, data) => {
	if (typeof data === 'string') {
		data = JSON.parse(data);
		project.initData(data || []);
	}
})
ipcRenderer.on('run-test-process', (sender, res) => {
	const { fail, data = {} } = res;
	const { index } = data;
	project.eventActive = index;
	if (fail) {
		project.projects[project.projectActive].pages[project.pageActive].tests[project.testActive].events[index].fail = fail;
	}
})


export default project